<?php 
	include_once('connection.php');
	include_once('session.php');
	//Reload to get posts
	if ($_POST['action'] == 'reload') {
		include_once('../functions/posts.php');
		include_once('../functions/pagination.php');
		get_posts($_POST['page'],10,$_POST['conditions']);

	}
	else if ($_POST['action'] == 'update') {
		include_once('../functions/posts.php');
		include_once('../functions/pagination.php');
		$res =  update_posts_state($_POST['update_conditions'],$_POST['value']);
		//Update successful
		if ($res > 0) {
			//Reload the posts page 1
			get_posts(1,10,$_POST['conditions']);
		}
	}
	else if ($_POST['action'] == 'delete') {
		include_once('../functions/posts.php');
		include_once('../functions/pagination.php');
		$res = delete_posts($_POST['update_conditions']);
		//Delete successful
		if ($res > 0) {
			//Reload the posts page 1
			get_posts(1,10,$_POST['conditions']);
		}
	}
	else {
		include_once('functions/posts.php');
		include_once('functions/pagination.php');
	}
	function get_posts($page,$posts_per_page,$conditions = '') {
		$rows = array();
		if ($conditions == '')
			$conditions = 'WHERE p.state=1 OR p.state=2';
		$posts_per_page = 5; //Set posts_per_page
		$from = ($page - 1)*$posts_per_page;
		$rows = get_posts_with_conditions($from,$posts_per_page,$conditions);
		$numPosts = count_posts_with_conditions($conditions);
		create_table($rows,$numPosts,$from,$posts_per_page);
	}

	function create_table($rows,$numPosts,$from,$posts_per_page) {
		echo '<table width="100%" cellspacing="0" cellpadding="0" border="0" name="posts-table" id="posts-table">
				<thead>
					<tr>
						<th scope="col" width="4%"><input type="checkbox" id="checkall" onclick="checkAll(this);" /></th>
						<th scope="col" class="left">Title</th>
						<th scope="col" width="8%">Publish</th>
						<th scope="col" width="20%">Category</th>
						<th scope="col" width="12%">Created by</th>
						<th scope="col" width="10%">Date</th>
						<th scope="col" width="7%">Views</th>
					</tr>
				</thead>';
		echo '<tfoot>';
		echo '<tr><td colspan="7"><div id="pagination">';
		pagination($numPosts,$from,$posts_per_page);
		echo '</div></td></tr>';
		echo '</tfoot>';
		echo '<tbody>';
		$i = 1;
		foreach ($rows as $row) {
			if ($row[state] == '1') {
				$state = "publish";
			} else {
				$state = "unpublish";
			}
			$date = date_create($row[created_on]);
			if ($i == 1) {
				echo '<tr>';
				$i = 0;
			}
			else {
				echo '<tr class="even">';
				$i = 1;
			}
			echo '<td><input type="checkbox" value="'.$row[id].'" name="cb[]" onclick="isChecked(this.checked);" /></td>';
			echo '<td class="left"><a href="create-posts.php?id='.$row[id].'">'.$row[title].'</a></td>';
			echo '<td><div class="state '.$state.'" onclick="update_post_state(this);"><span>'.$state.'</span></div></td>';
			echo '<td>'.$row[category_name].'</td>';
			echo '<td>'.$row[created_by].'</td>';
			echo '<td>'.date_format($date,'d-m-Y').'</td>';
			echo '<td>10</td>';
			echo '</tr>';
		}
		echo '</tbody>';
		echo '</table>';
	}
?>